package cn.iocoder.yudao.module.wine.service.supplyorder;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
import cn.iocoder.yudao.module.system.api.dict.dto.DictDataRespDTO;
import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictDataDO;
import cn.iocoder.yudao.module.system.service.dict.DictDataService;
import cn.iocoder.yudao.module.trade.api.brokerage.BrokerageUserApi;
import cn.iocoder.yudao.module.trade.api.brokerage.dto.BrokerageUserDTO;
import cn.iocoder.yudao.module.wine.controller.admin.businessuser.vo.BusinessUserRespVO;
import cn.iocoder.yudao.module.wine.controller.admin.messageboard.vo.MessageBoardSaveReqVO;
import cn.iocoder.yudao.module.wine.controller.admin.supplyinfo.vo.SupplyInfoRespVO;
import cn.iocoder.yudao.module.wine.controller.admin.supplyorder.utils.OrderStatusEnum;
import cn.iocoder.yudao.module.wine.controller.app.businessuser.vo.AppUserAddressRespVO;
import cn.iocoder.yudao.module.wine.controller.app.supplyinfo.enums.SupplyInfoEnum;
import cn.iocoder.yudao.module.wine.controller.app.supplyinfo.vo.AppCreateOrderReqVO;
import cn.iocoder.yudao.module.wine.controller.app.supplyinfo.vo.AppSupplyInfoItemReqVO;
import cn.iocoder.yudao.module.wine.controller.app.supplyorder.enums.SupplyOrderEnum;
import cn.iocoder.yudao.module.wine.controller.app.supplyorder.vo.*;
import cn.iocoder.yudao.module.wine.controller.app.supplyorderlog.enums.SupplyOrderLogEnum;
import cn.iocoder.yudao.module.wine.dal.dataobject.businesscredit.BusinessCreditDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.businessuser.BusinessUserDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.complaininfo.ComplainInfoDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.membertype.MemberTypeDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.messageboard.MessageBoardDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.premiumrecord.PremiumRecordDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplyinfo.SupplyInfoDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplyorderevaluate.SupplyOrderEvaluateDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplyorderitem.SupplyOrderItemDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplyorderlog.SupplyOrderLogDO;
import cn.iocoder.yudao.module.wine.dal.mysql.complaininfo.ComplainInfoMapper;
import cn.iocoder.yudao.module.wine.dal.mysql.messageboard.MessageBoardMapper;
import cn.iocoder.yudao.module.wine.dal.mysql.premiumrecord.PremiumRecordMapper;
import cn.iocoder.yudao.module.wine.dal.mysql.supplyinfo.SupplyInfoMapper;
import cn.iocoder.yudao.module.wine.dal.mysql.supplyorderevaluate.SupplyOrderEvaluateMapper;
import cn.iocoder.yudao.module.wine.dal.mysql.supplyorderitem.SupplyOrderItemMapper;
import cn.iocoder.yudao.module.wine.dal.mysql.supplyorderlog.SupplyOrderLogMapper;
import cn.iocoder.yudao.module.wine.service.businessuser.BusinessUserService;
import cn.iocoder.yudao.module.wine.service.complaininfo.ComplainInfoService;
import cn.iocoder.yudao.module.wine.service.messageboard.MessageBoardService;
import cn.iocoder.yudao.module.wine.utils.SnowflakeIdGeneratorUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.wine.controller.admin.supplyorder.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplyorder.SupplyOrderDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.supplyorder.SupplyOrderMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getTerminal;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 供求订单管理 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class SupplyOrderServiceImpl implements SupplyOrderService {

    //region 代码块
    @Resource
    private SupplyOrderMapper supplyOrderMapper;

    @Resource
    private DictDataService dictDataService;

    @Resource
    private BrokerageUserApi brokerageUserApi;

    @Resource
    private MemberUserApi memberUserApi;

    @Resource
    private SupplyOrderLogMapper supplyOrderLogMapper;

    @Resource
    private SupplyOrderItemMapper supplyOrderItemMapper;

    @Resource
    private SupplyInfoMapper supplyInfoMapper;

    @Resource
    private MessageBoardService messageBoardService;

    @Resource
    private DictDataApi dataApi;

    @Resource
    private ComplainInfoMapper complainInfoMapper;

    @Resource
    private SupplyOrderEvaluateMapper supplyOrderEvaluateMapper;

    @Resource
    private BusinessUserService businessUserService;

    @Resource
    private PremiumRecordMapper premiumRecordMapper;
    //endregion

    @Override
    public Long createSupplyOrder(SupplyOrderSaveReqVO createReqVO) {
        // 插入
        SupplyOrderDO supplyOrder = BeanUtils.toBean(createReqVO, SupplyOrderDO.class);
        supplyOrderMapper.insert(supplyOrder);
        // 返回
        return supplyOrder.getId();
    }

    /**
     * 后台操作更新订单信息
     * @param updateReqVO 更新信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSupplyOrder(SupplyOrderSaveReqVO updateReqVO) {
        // 校验存在
        validateSupplyOrderExists(updateReqVO.getId());
        // 更新
        SupplyOrderDO supplyOrderDO = BeanUtils.toBean(updateReqVO, SupplyOrderDO.class);
        SupplyOrderDO source = supplyOrderMapper.selectById(updateReqVO.getId());

        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        //后台操作已打款给卖家 或 已退款给买家   表示完成
        if (SupplyOrderEnum.REMITTED_MONEY.getCode().equals(updateReqVO.getOperateStatus()) || SupplyOrderEnum.REFUNDED_TO_BUYER.getCode().equals(updateReqVO.getOperateStatus())) {

            List<SupplyOrderItemDO> supplyOrderItemListByOrderId = supplyOrderItemMapper.getSupplyOrderItemListByOrderId(updateReqVO.getId());
            List<Long> collect = supplyOrderItemListByOrderId.stream().map(item -> item.getInfoId()).collect(Collectors.toList());
            List<SupplyInfoDO> supplyInfoDOS = supplyInfoMapper.selectList(new LambdaQueryWrapperX<SupplyInfoDO>().inIfPresent(SupplyInfoDO::getId, collect));
            List<SupplyInfoDO> supplyInfoDOList = supplyInfoDOS.stream().peek(item -> {
                item.setTradeStatus(SupplyInfoEnum.SELL_OUT.getCode());
            }).collect(Collectors.toList());
            //信息售罄
            supplyInfoMapper.updateBatch(supplyInfoDOList);
        }

        //后台操作已收买家货款,则要把订单状态修改成待发货
        if (SupplyOrderEnum.PAYMENT_HAS_BEEN_RECEIVED.getCode().equals(supplyOrderDO.getOperateStatus())) {
            //订单状态修改成待发货,货款支付状态改成已付货款

            supplyOrderDO.setStatus(SupplyOrderEnum.WAIT_FOR_SENDING.getCode());
            supplyOrderDO.setPayStatus(1);
            supplyOrderDO.setPayChannelCode("offline");//线下收款
            supplyOrderDO.setPayTime(LocalDateTime.now());

            //插入操作订单的日志
            SupplyOrderLogDO log = new SupplyOrderLogDO();
            log.setUserId(loginUserId);
            log.setUserType(UserTypeEnum.ADMIN.getName());
            log.setOperateType(1);
            log.setOrderId(supplyOrderDO.getId());
            //获取订单状态字典
            List<DictDataDO> orderStatus = dictDataService.getDictDataListByDictType("order_status");
            for (int i = 0; i < orderStatus.size(); i++) {
                DictDataDO dictDataDO = orderStatus.get(i);
                if (dictDataDO.getValue().equals(source.getStatus().toString())) {
                    log.setBeforeStatus(dictDataDO.getLabel());
                }
                if (dictDataDO.getValue().equals(supplyOrderDO.getStatus().toString())) {
                    log.setAfterStatus(dictDataDO.getLabel());
                }
            }
            log.setContent(UserTypeEnum.ADMIN.getName() + SupplyOrderLogEnum.UPDATE_STATUS.getContent() + log.getBeforeStatus() + "->" + log.getAfterStatus());
            supplyOrderLogMapper.insert(log);
        }

        //记录后台操作订单的日志
        SupplyOrderLogDO log = new SupplyOrderLogDO();
        log.setUserId(loginUserId);
        log.setUserType(UserTypeEnum.ADMIN.getName());
        log.setOrderId(supplyOrderDO.getId());
        //获取订单状态字典
        List<DictDataDO> operateStatus = dictDataService.getDictDataListByDictType("operate_status");
        for (int i = 0; i < operateStatus.size(); i++) {
            DictDataDO dictDataDO = operateStatus.get(i);
            if (dictDataDO.getValue().equals(source.getOperateStatus().toString())) {
                log.setBeforeStatus(dictDataDO.getLabel());
            }
            if (dictDataDO.getValue().equals(supplyOrderDO.getOperateStatus().toString())) {
                log.setAfterStatus(dictDataDO.getLabel());
            }
        }
        log.setContent(UserTypeEnum.ADMIN.getName() + SupplyOrderLogEnum.OPERATE_UPDATE_STATUS.getContent() + log.getBeforeStatus() + "->" + log.getAfterStatus());
        supplyOrderLogMapper.insert(log);

        //修改订单信息
        supplyOrderMapper.updateById(supplyOrderDO);
    }

    /**
     * 付担保费后 更新订单信息
     * @param supplyOrderDO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void payUpdateSupplyOrder(SupplyOrderDO supplyOrderDO,Long curTenantId,Long payUserId) {
        // 校验存在
        validateSupplyOrderExists(supplyOrderDO.getId());
        // 更新
        SupplyOrderDO source = supplyOrderMapper.selectById(supplyOrderDO.getId());

        //记录订单日志
        if (payUserId > 0) {
            SupplyOrderLogDO log = new SupplyOrderLogDO();
            log.setUserId(payUserId);
            log.setUserType(UserTypeEnum.MEMBER.getName());
            log.setOperateType(0);
            log.setOrderId(supplyOrderDO.getId());
            log.setTenantId(curTenantId);

            log.setBeforeStatus("未付款");
            log.setAfterStatus("已付担保费");
            log.setContent(UserTypeEnum.MEMBER.getName() + "(" + log.getUserId() + ")" + SupplyOrderLogEnum.UPDATE_STATUS.getContent() + log.getBeforeStatus() + "->" + log.getAfterStatus());

            log.setCreator(payUserId.toString());
            supplyOrderLogMapper.insert(log);
        }

        if (SupplyOrderEnum.ACCOUNT_PAID.getCode().equals(supplyOrderDO.getStatus()) &&
                !Objects.equals(source.getStatus(), supplyOrderDO.getStatus()) &&
                payUserId==0L) {

            SupplyOrderLogDO log = new SupplyOrderLogDO();
            log.setUserId(0L);
            log.setUserType("系统自动");
            log.setOperateType(0);
            log.setOrderId(supplyOrderDO.getId());
            log.setTenantId(curTenantId);
            log.setCreator(payUserId.toString());
            //获取订单状态字典
            List<DictDataDO> orderStatus = dictDataService.getDictDataListByDictType("order_status");
            for (int i = 0; i < orderStatus.size(); i++) {
                DictDataDO dictDataDO = orderStatus.get(i);
                if (dictDataDO.getValue().equals(source.getStatus().toString())) {
                    log.setBeforeStatus(dictDataDO.getLabel());
                }
                if (dictDataDO.getValue().equals(supplyOrderDO.getStatus().toString())) {
                    log.setAfterStatus(dictDataDO.getLabel());
                }
            }
            log.setContent("系统根据担保费支付情况" + SupplyOrderLogEnum.UPDATE_STATUS.getContent() + log.getBeforeStatus() + "->" + log.getAfterStatus());
            supplyOrderLogMapper.insert(log);

            //更新订单信息
            supplyOrderMapper.updateById(supplyOrderDO);
        }

    }

    @Override
    public void deleteSupplyOrder(Long id) {
        // 校验存在
        validateSupplyOrderExists(id);
        // 删除
        supplyOrderMapper.deleteById(id);
    }

    private SupplyOrderDO validateSupplyOrderExists(Long id) {
        SupplyOrderDO data = supplyOrderMapper.selectById(id);
        if (data == null) {
            throw exception(INFO_NOT_EXISTS);
        } else {
            return data;
        }
    }

    @Override
    public SupplyOrderDO getSupplyOrder(Long id) {
        return supplyOrderMapper.selectById(id);
    }

    @Override
    public PageResult<SupplyOrderDO> getSupplyOrderPage(SupplyOrderPageReqVO pageReqVO) {
        return supplyOrderMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<SupplyOrderRespVO> getSupplyOrderNewPage(SupplyOrderPageReqVO pageReqVO) {
        return supplyOrderMapper.selectNewPage(pageReqVO);
    }

    @Override
    public void updateSupplyOrderStatus(SupplyOrderSaveReqVO updateReqVO) {
        // 校验数据是否存在
        validateSupplyOrderExists(updateReqVO.getId());
        SupplyOrderDO bean = BeanUtils.toBean(updateReqVO, SupplyOrderDO.class);

        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        SupplyOrderLogDO supplyOrderLogDO = new SupplyOrderLogDO();
        supplyOrderLogDO.setUserId(loginUserId);
        supplyOrderLogDO.setOrderId(updateReqVO.getId());
        supplyOrderLogDO.setUserType(UserTypeEnum.ADMIN.getName());
        supplyOrderLogDO.setOperateType(1);
        supplyOrderLogDO.setContent("id=" + loginUserId + "管理员修改了订单");
        supplyOrderLogMapper.insert(supplyOrderLogDO);

        supplyOrderMapper.updateById(bean);
    }

    /**
     * 取消订单
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelOrder(Long id) {
        SupplyOrderDO supplyOrderDO = validateSupplyOrderExists(id);
        supplyOrderDO.setStatus(OrderStatusEnum.CANCEL.getCode());
        supplyOrderMapper.updateById(supplyOrderDO);
        //记录订单日志
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        SupplyOrderLogDO log = new SupplyOrderLogDO();
        log.setUserId(loginUserId);
        log.setUserType(UserTypeEnum.ADMIN.getName());
        log.setOrderId(supplyOrderDO.getId());
        //获取订单状态字典
        List<DictDataDO> orderStatus = dictDataService.getDictDataListByDictType("order_status");
        for (int i = 0; i < orderStatus.size(); i++) {
            DictDataDO dictDataDO = orderStatus.get(i);
            if (dictDataDO.getValue().equals(supplyOrderDO.getStatus().toString())) {
                log.setBeforeStatus(dictDataDO.getLabel());
            }
            if (dictDataDO.getValue().equals(OrderStatusEnum.CANCEL.getCode().toString())) {
                log.setAfterStatus(dictDataDO.getLabel());
            }
        }
        log.setContent(UserTypeEnum.ADMIN.getName() + SupplyOrderLogEnum.CANCEL_ORDER.getContent());
        supplyOrderLogMapper.insert(log);
    }

    @Override
    public List<AppSupplyOrderRespVO> selectOrderList(AppSupplyOrderListReqVO reqVO) {
        List<Integer> integers = new ArrayList<>();
        if (reqVO.getDifferentiate() != null) {
            //differentiate=1:正常交易、担责交易、担责担保交易
            if (reqVO.getDifferentiate().equals(SupplyOrderEnum.NORMAL.getCode())) {
                integers.add(SupplyOrderEnum.NORMAL.getCode());
                integers.add(SupplyOrderEnum.TAKE_RESPONSIBILITY.getCode());
                integers.add(SupplyOrderEnum.TAKE_RESPONSIBILITY_GUARANTEE.getCode());
            } else {
                //differentiate=2:担保交易、担责担保交易
                integers.add(SupplyOrderEnum.GUARANTEE.getCode());
                integers.add(SupplyOrderEnum.TAKE_RESPONSIBILITY_GUARANTEE.getCode());
            }
        }
        //differentiate=2:担保交易、担责担保交易
        if (reqVO.getDifferentiate().equals(SupplyOrderEnum.GUARANTEE.getCode())) {
            return selectListItem(reqVO);
        }
        //differentiate=1:正常交易、担责交易、担责担保交易
        return supplyOrderMapper.selectListItem2(integers, reqVO);

    }

    /**
     *买家申请退货
     * @param reqVO
     * @return
     */
    @Override
    public Long salesReturn(AppSalesReturnReqVO reqVO) {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        // 校验数据是否存在
        SupplyOrderDO supplyOrderDO = validateSupplyOrderExists(reqVO.getId());
        if (!supplyOrderDO.getBuyUserId().equals(loginUserId)) {
            throw exception(HAVE_NO_RIGHT);
        }
        //写入日志
        SupplyOrderLogDO log = new SupplyOrderLogDO();
        log.setUserId(loginUserId);
        log.setUserType(UserTypeEnum.MEMBER.getName());
        log.setOrderId(supplyOrderDO.getId());
        log.setOperateType(0);
        //获取订单状态字典
        List<DictDataDO> orderStatus = dictDataService.getDictDataListByDictType("order_status");
        for (int i = 0; i < orderStatus.size(); i++) {
            DictDataDO dictDataDO = orderStatus.get(i);
            if (dictDataDO.getValue().equals(supplyOrderDO.getStatus().toString())) {
                log.setBeforeStatus(dictDataDO.getLabel());
            }
            log.setAfterStatus(SupplyOrderEnum.UNDER_RETURN_APPLICATION.getContent());
        }
        log.setContent(UserTypeEnum.MEMBER.getName() + SupplyOrderLogEnum.UPDATE_STATUS.getContent() + log.getBeforeStatus() + "->" + log.getAfterStatus());
        supplyOrderLogMapper.insert(log);

        //申请退货提交的信息
        supplyOrderDO.setBuyUserRemark(reqVO.getBuyUserRemark());
        if (reqVO.getSalesImages() != null) {
            supplyOrderDO.setSalesImages(reqVO.getSalesImages());
        }
        if (reqVO.getReceiverVideo() != null) {
            supplyOrderDO.setReceiverVideo(reqVO.getReceiverVideo());
        }
        if (reqVO.getOperateStatus() != null) {
            supplyOrderDO.setOperateStatus(reqVO.getOperateStatus());
        }
        supplyOrderDO.setStatus(SupplyOrderEnum.UNDER_RETURN_APPLICATION.getCode());

        supplyOrderMapper.updateById(supplyOrderDO);

        return supplyOrderDO.getId();

    }

    /**
     * 生成担责订单
     * @param reqVO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean placeAnOrder(AppCreateOrderReqVO reqVO) {

        List<AppSupplyInfoItemReqVO> supplyInfoData = reqVO.getSupplyInfoData();

        // 创建订单
        SupplyOrderDO supplyOrderDO = new SupplyOrderDO();
        //雪花算法 ->订单号
        String orderNo = SnowflakeIdGeneratorUtil.generateId();
        //订单号
        supplyOrderDO.setNo(orderNo);
        //订单类型 出售，求购，套现
        supplyOrderDO.setTerminal(getTerminal());

        //卖方用户id
        String saleUserMobile = reqVO.getSaleUserMobile();
        MemberUserRespDTO saleUser = memberUserApi.getUserByMobile(saleUserMobile);
        supplyOrderDO.setSaleUserId(saleUser.getId());

        //买方用户id
        String buyUserMobile = reqVO.getBuyUserMobile();
        MemberUserRespDTO buyUser = memberUserApi.getUserByMobile(buyUserMobile);
        supplyOrderDO.setBuyUserId(buyUser.getId());

        //推广人id
        BrokerageUserDTO brokerageUserDTO = brokerageUserApi.getBindUserId(saleUser.getId());
        Long bindUserId = brokerageUserDTO.getBindUserId();
        supplyOrderDO.setBrokerageUserId(bindUserId);

        BigDecimal bigDecimal = new BigDecimal(100);

        //非担保交易，无需保费金额
        supplyOrderDO.setPremiumPrice(0);

        //设置地址
        supplyOrderDO.setReceiverMobile(reqVO.getBuyUserMobile());
        supplyOrderDO.setReceiverName(reqVO.getBuyUserName());
        supplyOrderDO.setReceiverCompanyName(reqVO.getBuyCompanyName());
        supplyOrderDO.setReceiverDetailAddress(reqVO.getReceiveAddress());

        //上传信息
        supplyOrderDO.setLogisticsId(reqVO.getLogisticsId());
        supplyOrderDO.setLogisticsNo(reqVO.getLogisticsNo());
        supplyOrderDO.setDeliveryImages(reqVO.getDeliveryImages());
        supplyOrderDO.setDeliveryVideo(reqVO.getDeliveryVideo());

        supplyOrderDO.setSalesLogisticsNo(reqVO.getSalesLogisticsNo());
        supplyOrderDO.setSalesImages(reqVO.getSalesImages());

        int insert = supplyOrderMapper.insert(supplyOrderDO);

        //创建供求订单项列表
        for (int i = 0; i < supplyInfoData.size(); i++) {
            SupplyInfoDO supplyInfoDO = supplyInfoMapper.selectOne(new LambdaQueryWrapperX<SupplyInfoDO>().eq(SupplyInfoDO::getId, supplyInfoData.get(i).getSupplyInfoId()));
            if (!SupplyInfoEnum.NO_TRADE.getCode().equals(supplyInfoDO.getTradeStatus())) {
                throw exception(IN_TRADING);
            }
            if (!StringUtils.isEmpty(reqVO.getClause())) {
                supplyInfoDO.setClause(reqVO.getClause());
            }
            //修改供求信息交易状态为 担责交易中或正常交易中
            if (SupplyInfoEnum.IS_DUTY.getCode().equals(supplyInfoDO.getIsDuty())) {
                supplyInfoDO.setTradeStatus(SupplyInfoEnum.LIABILITY_TRANSACTION.getCode());
            } else {
                supplyInfoDO.setTradeStatus(SupplyInfoEnum.REGULAR_WAY.getCode());
            }
            //变更供求信息:减数量、改价格
            supplyInfoDO.setPackages(supplyInfoData.get(i).getPackages() == null ? supplyInfoDO.getPackages() : supplyInfoData.get(i).getPackages());
            supplyInfoDO.setSalePrice(supplyInfoData.get(i).getSalePrice() == null ? supplyInfoDO.getSalePrice() : supplyInfoData.get(i).getSalePrice());
            supplyInfoMapper.updateById(supplyInfoDO);

            supplyOrderDO.setType(supplyInfoDO.getInfoType());
            supplyOrderDO.setTradeType(supplyInfoDO.getTradeStatus());

            //创建供求订单项信息
            BigDecimal multiply = supplyInfoDO.getSalePrice().multiply(new BigDecimal(supplyInfoDO.getPackages())).multiply(new BigDecimal(supplyInfoDO.getQuantity()));
            SupplyOrderItemDO supplyOrderItemDO = new SupplyOrderItemDO();
            supplyOrderItemDO.setPackages(supplyInfoDO.getPackages());
            supplyOrderItemDO.setPremiumPrice(0);
            supplyOrderItemDO.setPayPrice(multiply);
            supplyOrderItemDO.setSalePrice(supplyInfoDO.getSalePrice());
            supplyOrderItemDO.setOrderId(supplyOrderDO.getId());
            supplyOrderItemDO.setInfoId(supplyInfoDO.getId());
            supplyOrderItemDO.setQuantity(supplyInfoDO.getQuantity());
            supplyOrderItemMapper.insert(supplyOrderItemDO);

            //TODO  记录日志
            Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
            if (loginUserId == null) {
                throw exception(NOT_LOGIN);
            }

            if (loginUserId.equals(buyUser.getId())) {
                SupplyOrderLogDO supplyOrderLogDO = new SupplyOrderLogDO();
                supplyOrderLogDO.setOrderId(supplyOrderDO.getId());
                supplyOrderLogDO.setUserId(loginUserId);
                supplyOrderLogDO.setUserType(UserTypeEnum.MEMBER.getName());
                supplyOrderLogDO.setOperateType(0);
                supplyOrderLogDO.setContent(SupplyOrderLogEnum.BUYER_AN_ORDER_IS_CREATED.getContent());
                supplyOrderLogMapper.insert(supplyOrderLogDO);
            }
            if (loginUserId.equals(saleUser.getId())) {
                SupplyOrderLogDO supplyOrderLogDO = new SupplyOrderLogDO();
                supplyOrderLogDO.setOrderId(supplyOrderDO.getId());
                supplyOrderLogDO.setUserId(loginUserId);
                supplyOrderLogDO.setUserType(UserTypeEnum.MEMBER.getName());
                supplyOrderLogDO.setOperateType(0);
                supplyOrderLogDO.setContent(SupplyOrderLogEnum.SELLER_AN_ORDER_IS_CREATED.getContent());
                supplyOrderLogMapper.insert(supplyOrderLogDO);
            }
            //创建留言消息
            MessageBoardSaveReqVO messageBoardSaveReqVO = new MessageBoardSaveReqVO();
            messageBoardSaveReqVO.setContent("担责订单已生成");
            messageBoardSaveReqVO.setTypeId(0);
            messageBoardSaveReqVO.setInfoId(0L);
            messageBoardSaveReqVO.setSendId(saleUser.getId());
            messageBoardSaveReqVO.setReceiveId(buyUser.getId());
            messageBoardSaveReqVO.setChatId(SnowflakeIdGeneratorUtil.generateLongId());
            messageBoardService.createOrderMessageBoard(messageBoardSaveReqVO);
        }

        List<SupplyOrderItemDO> list = supplyOrderItemMapper.getSupplyOrderItemListByOrderId(supplyOrderDO.getId());
        BigDecimal price = list.stream().map(SupplyOrderItemDO::getPayPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
        Integer quantity = list.stream().map(SupplyOrderItemDO::getQuantity).reduce(0, Integer::sum);

        //Integer premiumPrice = list.stream().map(SupplyOrderItemDO::getPremiumPrice).reduce(0, Integer::sum);

        Integer packages = list.stream().map(SupplyOrderItemDO::getPackages).reduce(0, Integer::sum);
        //商品原价 单位分 所以要*100
        supplyOrderDO.setTotalPrice(price.multiply(bigDecimal).intValue());
        //应付金额（总），单位：分
        supplyOrderDO.setPayPrice(price.multiply(bigDecimal).intValue());
        supplyOrderDO.setProductCount(packages);
        supplyOrderDO.setProductQuantity(quantity);
        supplyOrderMapper.updateById(supplyOrderDO);

        return true;
    }

    /**
     * 获取供应订单信息
     * @param id
     * @return
     */
    @Override
    public AppSupplyOrderRespVO appGetSupplyOrder(Long id) {

        AppSupplyOrderRespVO appSupplyOrder = supplyOrderMapper.getAppSupplyOrder(id);
        if (appSupplyOrder != null) {
            Long loginUserId = SecurityFrameworkUtils.getLoginUserId();

            if (loginUserId.equals(appSupplyOrder.getSaleUserId())) {
                appSupplyOrder.setIdentity(SupplyOrderEnum.SELLER.getCode());
            } else {
                appSupplyOrder.setIdentity(SupplyOrderEnum.BUYER.getCode());
            }

            //聊天信息
            Long chatId = messageBoardService.getChatId(appSupplyOrder.getBuyUserId(), appSupplyOrder.getSaleUserId());
            if (chatId != null) {
                appSupplyOrder.setChatId(chatId);
            }
            //投诉曝光信息
            ComplainInfoDO complainInfo = complainInfoMapper.selectByInfoId(id);
            if (complainInfo != null) {
                appSupplyOrder.setComplainId(complainInfo.getId());
            }
            //当前用户的评价
            SupplyOrderEvaluateDO supplyOrderEvaluateInfo = supplyOrderEvaluateMapper.selectByOrderId(id, loginUserId);
            if (supplyOrderEvaluateInfo != null) {
                appSupplyOrder.setEvaluateId(supplyOrderEvaluateInfo.getId());
            }
            //商家类型:法人代表
            List<DictDataRespDTO> merchantType = dataApi.getDictDataList("merchant_type");
            BusinessUserRespVO businessSaleUser = businessUserService.appGetBusinessUser(appSupplyOrder.getSaleUserId());
            BusinessUserRespVO businessBuyUser = businessUserService.appGetBusinessUser(appSupplyOrder.getBuyUserId());
            merchantType.forEach(dict -> {
                if (dict.getValue().equals(String.valueOf(businessSaleUser.getBusinessType()))) {
                    appSupplyOrder.setSaleBusinessTypeName(dict.getLabel());
                }
                if (dict.getValue().equals(String.valueOf(businessBuyUser.getBusinessType()))) {
                    appSupplyOrder.setBuyBusinessTypeName(dict.getLabel());
                }
            });

        }
        return appSupplyOrder;
    }

    /**
     * 卖家提交发货、买家提交退货、更新订单完成 使用
     * 更新订单
     * @param reqVO
     * @return
     */
    @Override
    public Long appUpdateSupplyOrder(AppSupplyOrderUpdateReqVO reqVO) {
        SupplyOrderDO bean = BeanUtils.toBean(reqVO, SupplyOrderDO.class);
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();

        SupplyOrderDO supplyOrderDO = supplyOrderMapper.selectById(reqVO.getId());

        //写入订单日志
        SupplyOrderLogDO log = new SupplyOrderLogDO();
        log.setUserId(loginUserId);
        log.setUserType(UserTypeEnum.MEMBER.getName());
        log.setOrderId(reqVO.getId());
        log.setOperateType(0);

        List<DictDataDO> orderStatus = dictDataService.getDictDataListByDictType("order_status");
        for (int i = 0; i < orderStatus.size(); i++) {
            DictDataDO dictDataDO = orderStatus.get(i);
            if (dictDataDO.getValue().equals(supplyOrderDO.getStatus().toString())) {
                log.setBeforeStatus(dictDataDO.getLabel());
            }
            log.setAfterStatus(SupplyOrderEnum.WAIT_FOR_RECEIVING.getContent());
        }
        //有发货物流单号，则表示卖家发货，待买家收货
        if (!StringUtils.isEmpty(reqVO.getLogisticsNo())) {
            //卖家发货，待买家收货
            bean.setStatus(SupplyOrderEnum.WAIT_FOR_RECEIVING.getCode());
            bean.setDeliveryTime(LocalDateTime.now());

            log.setContent(UserTypeEnum.MEMBER.getName() + SupplyOrderLogEnum.UPDATE_STATUS.getContent() + log.getBeforeStatus() + "->" + log.getAfterStatus());
            supplyOrderLogMapper.insert(log);
        }
        //有退货物流单号，则表示买家退货，待卖家收货
        if (!StringUtils.isEmpty(reqVO.getSalesLogisticsNo())) {
            //买家退货，待卖家收货
            bean.setStatus(SupplyOrderEnum.REMITTED_MONEY_STATUS.getCode());
            bean.setSalesTime(LocalDateTime.now());

            log.setContent(UserTypeEnum.MEMBER.getName() + SupplyOrderLogEnum.UPDATE_STATUS.getContent() + log.getBeforeStatus() + "->" + log.getAfterStatus());
            supplyOrderLogMapper.insert(log);
        }
        //如果已完成
        if (Objects.equals(SupplyOrderEnum.COMPLETED.getCode(), reqVO.getStatus())) {
            bean.setFinishTime(LocalDateTime.now());

            log.setAfterStatus(SupplyOrderEnum.COMPLETED.getContent());
            log.setContent(UserTypeEnum.MEMBER.getName() + SupplyOrderLogEnum.UPDATE_STATUS.getContent() + log.getBeforeStatus() + "->" + log.getAfterStatus());
            supplyOrderLogMapper.insert(log);
        }
        //更新订单信息
        supplyOrderMapper.updateById(bean);

        return bean.getId();

    }

    /**
     * 买家确认收货
     * @param reqVO
     * @return
     */
    @Override
    public Long confirmReceipt(AppSalesReturnReqVO reqVO) {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        // 校验数据是否存在
        SupplyOrderDO supplyOrderDO = validateSupplyOrderExists(reqVO.getId());
        if (!supplyOrderDO.getBuyUserId().equals(loginUserId)) {
            throw exception(HAVE_NO_RIGHT);
        }

        //写入日志
        SupplyOrderLogDO log = new SupplyOrderLogDO();
        log.setUserId(loginUserId);
        log.setUserType(UserTypeEnum.MEMBER.getName());
        log.setOrderId(supplyOrderDO.getId());
        log.setOperateType(0);

        //获取订单状态字典
        List<DictDataDO> orderStatus = dictDataService.getDictDataListByDictType("order_status");
        for (int i = 0; i < orderStatus.size(); i++) {
            DictDataDO dictDataDO = orderStatus.get(i);
            if (dictDataDO.getValue().equals(supplyOrderDO.getStatus().toString())) {
                log.setBeforeStatus(dictDataDO.getLabel());
            }
            log.setAfterStatus(SupplyOrderEnum.COMPLETED.getContent());
        }
        log.setContent(UserTypeEnum.MEMBER.getName() + SupplyOrderLogEnum.UPDATE_STATUS.getContent() + log.getBeforeStatus() + "-> 已收货，订单完成！");
        supplyOrderLogMapper.insert(log);

        //买家确认收货提交信息
        //如果有传则更新
        supplyOrderDO.setOperateStatus(reqVO.getOperateStatus() == null ? supplyOrderDO.getOperateStatus() : reqVO.getOperateStatus());
        supplyOrderDO.setStatus(SupplyOrderEnum.COMPLETED.getCode());
        supplyOrderDO.setReceiveTime(LocalDateTime.now());
        if (reqVO.getReceiverVideo() != null) {
            supplyOrderDO.setReceiverVideo(reqVO.getReceiverVideo());
        }
        if (reqVO.getSalesImages() != null) {
            supplyOrderDO.setSalesImages(reqVO.getSalesImages());
        }
        if (reqVO.getBuyUserRemark() != null) {
            supplyOrderDO.setBuyUserRemark(reqVO.getBuyUserRemark());
        }
        supplyOrderMapper.updateById(supplyOrderDO);

        return supplyOrderDO.getId();

    }
    /**
     * 卖家确认收货
     * @param reqVO
     * @return
     */
    @Override
    public Long salesConfirm(AppSalesConfirmReqVO reqVO) {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        SupplyOrderDO supplyOrderDO = validateSupplyOrderExists(reqVO.getId());
        if (supplyOrderDO.getSaleUserId().equals(loginUserId)) {

            if (reqVO.getDeliveryImages() != null) {
                supplyOrderDO.setDeliveryImages(reqVO.getDeliveryImages());
            }
            if (reqVO.getDeliveryVideo() != null) {
                supplyOrderDO.setDeliveryVideo(reqVO.getDeliveryVideo());
            }
            //supplyOrderDO.setStatus(SupplyOrderEnum.RETURNED.getCode());
            supplyOrderDO.setStatus(reqVO.getStatus());
            if(Objects.equals(SupplyOrderEnum.RETURNED.getCode(), reqVO.getStatus())) {
                supplyOrderDO.setSaleReceiveTime(LocalDateTime.now());
            }

            supplyOrderMapper.updateById(supplyOrderDO);
            //写入日志
            SupplyOrderLogDO log = new SupplyOrderLogDO();
            log.setUserId(loginUserId);
            log.setUserType(UserTypeEnum.MEMBER.getName());
            log.setOrderId(supplyOrderDO.getId());
            log.setOperateType(0);
            log.setContent(SupplyOrderEnum.SELLER.getContent() + "确认收货！");
            supplyOrderLogMapper.insert(log);
        } else {
            throw exception(4000, "卖家才能确认收货!");
        }
        return supplyOrderDO.getId();
    }

    /**
     * 担责订单、担责担保订单，确认担责
     * @param reqVO
     * @return
     */
    @Override
    public Long affirmDuty(AppAffirmDutyReqVO reqVO) {

        SupplyOrderDO supplyOrderDO = validateSupplyOrderExists(reqVO.getId());
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();

        supplyOrderDO.setAffirmDuty(reqVO.getAffirmDuty());
        supplyOrderDO.setAffirmTime(LocalDateTime.now());
        //担责担保订单，确认担责后需要后台审核
        supplyOrderDO.setOperateStatus(reqVO.getOperateStatus() == null ? supplyOrderDO.getOperateStatus() : reqVO.getOperateStatus());

        supplyOrderDO.setDeliveryImages(StringUtils.isEmpty(reqVO.getDeliveryImages()) ? supplyOrderDO.getDeliveryImages() : reqVO.getDeliveryImages());
        supplyOrderDO.setLogisticsNo(StringUtils.isEmpty(reqVO.getLogisticsNo()) ? supplyOrderDO.getLogisticsNo() : reqVO.getLogisticsNo());

        supplyOrderDO.setSalesLogisticsNo(StringUtils.isEmpty(reqVO.getSalesLogisticsNo()) ? supplyOrderDO.getSalesLogisticsNo() : reqVO.getSalesLogisticsNo());
        supplyOrderDO.setSalesImages(StringUtils.isEmpty(reqVO.getSalesImages()) ? supplyOrderDO.getSalesImages() : reqVO.getSalesImages());

        //写入日志
        SupplyOrderLogDO log = new SupplyOrderLogDO();
        log.setUserId(loginUserId);
        log.setUserType(UserTypeEnum.MEMBER.getName());
        log.setOrderId(supplyOrderDO.getId());
        log.setOperateType(0);
        log.setContent(SupplyOrderEnum.BUYER.getContent() + "确认担责！");
        supplyOrderLogMapper.insert(log);

        supplyOrderMapper.updateById(supplyOrderDO);

        return supplyOrderDO.getId();
    }

    /**
     * 获取供应订单信息明细
     * @param id
     * @return
     */
    @Override
    public SupplyOrderRespVO getSupplyOrderInfo(Long id) {
        SupplyOrderDO supplyOrderDO = supplyOrderMapper.selectById(id);
        SupplyOrderRespVO bean = BeanUtils.toBean(supplyOrderDO, SupplyOrderRespVO.class);
        if (supplyOrderDO != null) {
            //卖家
            MemberUserRespDTO saleUser = memberUserApi.getUser(bean.getSaleUserId());
            bean.setSaleUserName(saleUser.getName());
            bean.setSaleUserNickName(saleUser.getNickname());
            AppUserAddressRespVO saleBusinessUser = businessUserService.appGetBusinessUserByMobile(saleUser.getMobile());
            if (saleBusinessUser != null) {
                bean.setSaleUserCompanyName(saleBusinessUser.getCompanyName());
                bean.setSaleUserCompanyAddress(saleBusinessUser.getCompanyAddress());
                bean.setSaleUserMobile(saleBusinessUser.getMobile());
            }
            //卖家支付保费
            if(supplyOrderDO.getPremiumPrice()>0){
                PremiumRecordDO saleRecord =  premiumRecordMapper.getRecordByUserId(bean.getSaleUserId(),id);
                if(saleRecord!=null){
                    bean.setSalePremiumStatus(saleRecord.getStatus());
                    bean.setSaleTradeNo(saleRecord.getTradeNo());
                    bean.setSaleTradeTime(saleRecord.getTradeTime());
                }
            }

            //买家
            MemberUserRespDTO buyUser = memberUserApi.getUser(bean.getBuyUserId());
            bean.setBuyUserName(buyUser.getName());
            bean.setBuyUserNickName(buyUser.getNickname());
            AppUserAddressRespVO buyBusinessUser = businessUserService.appGetBusinessUserByMobile(buyUser.getMobile());
            if (buyBusinessUser != null) {
                bean.setBuyUserCompanyName(buyBusinessUser.getCompanyName());
                bean.setBuyUserCompanyAddress(buyBusinessUser.getCompanyAddress());
                bean.setBuyUserMobile(buyBusinessUser.getMobile());
            }
            //买家支付保费
            if(supplyOrderDO.getPremiumPrice()>0) {
                PremiumRecordDO buyRecord = premiumRecordMapper.getRecordByUserId(bean.getBuyUserId(), id);
                if (buyRecord != null) {
                    bean.setBuyPremiumStatus(buyRecord.getStatus());
                    bean.setBuyTradeNo(buyRecord.getTradeNo());
                    bean.setBuyTradeTime(buyRecord.getTradeTime());
                }
            }
        }
        return bean;
    }

    /**
     * 卖家同意或拒绝退货
     * @param reqVO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long salesAudit(AppSalesAuditReqVO reqVO) {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();

        SupplyOrderDO supplyOrderDO = validateSupplyOrderExists(reqVO.getId());
        //如果有传则更新
        supplyOrderDO.setOperateStatus(reqVO.getOperateStatus() == null ? supplyOrderDO.getOperateStatus() : reqVO.getOperateStatus());

        if (SupplyOrderEnum.AFTER_SALE.getCode().equals(reqVO.getStatus())) {
            supplyOrderDO.setStatus(SupplyOrderEnum.AFTER_SALE.getCode());

            SupplyOrderLogDO log = new SupplyOrderLogDO();
            log.setUserId(loginUserId);
            log.setUserType(UserTypeEnum.MEMBER.getName());
            log.setOrderId(supplyOrderDO.getId());
            log.setOperateType(0);
            log.setContent(SupplyOrderEnum.SELLER.getContent() + "拒绝退货,订单状态改为售后中!");
            supplyOrderLogMapper.insert(log);
        }
        if (SupplyOrderEnum.RETURNING_GOODS.getCode().equals(reqVO.getStatus())) {
            supplyOrderDO.setStatus(SupplyOrderEnum.RETURNING_GOODS.getCode());
            SupplyOrderLogDO log = new SupplyOrderLogDO();
            log.setUserId(loginUserId);
            log.setUserType(UserTypeEnum.MEMBER.getName());
            log.setOrderId(supplyOrderDO.getId());
            log.setOperateType(0);
            log.setContent(SupplyOrderEnum.SELLER.getContent() + "同意退货,订单状态改为退货中!");
            supplyOrderLogMapper.insert(log);
        }
        supplyOrderMapper.updateById(supplyOrderDO);
        return supplyOrderDO.getId();
    }

    /**
     * 取消订单
     * @param reqVO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long cancel(AppCancelReqVO reqVO) {
        SupplyOrderDO supplyOrderDO = validateSupplyOrderExists(reqVO.getId());
        //取消订单
        supplyOrderDO.setStatus(SupplyOrderEnum.CANCEL.getCode());
        supplyOrderDO.setCancelType(1);//用户取消
        supplyOrderDO.setCancelTime(LocalDateTime.now());
        //写入日志
        SupplyOrderLogDO log = new SupplyOrderLogDO();
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        log.setUserId(loginUserId);
        log.setUserType(UserTypeEnum.MEMBER.getName());
        log.setOrderId(supplyOrderDO.getId());
        log.setOperateType(0);
        log.setContent(SupplyOrderEnum.BUYER.getContent() + "取消了订单!");
        supplyOrderLogMapper.insert(log);

        List<SupplyOrderItemDO> supplyOrderItemListByOrderId = supplyOrderItemMapper.getSupplyOrderItemListByOrderId(reqVO.getId());
        List<Long> collect = supplyOrderItemListByOrderId.stream().map(item -> item.getInfoId()).collect(Collectors.toList());
        List<SupplyInfoDO> supplyInfoDOS = supplyInfoMapper.selectList(new LambdaQueryWrapperX<SupplyInfoDO>().inIfPresent(SupplyInfoDO::getId, collect));
        List<SupplyInfoDO> collect1 = supplyInfoDOS.stream().map(item -> {
            item.setTradeStatus(SupplyInfoEnum.NO_TRADE.getCode());
            return item;
        }).collect(Collectors.toList());
        supplyInfoMapper.updateBatch(collect1);
        supplyOrderMapper.updateById(supplyOrderDO);
        return supplyOrderDO.getId();
    }

    /**
     * 担保交易、担责担保交易
     *
     * @param reqVO
     * @return
     */
    private List<AppSupplyOrderRespVO> selectListItem(AppSupplyOrderListReqVO reqVO) {
        //售后（退货处理中）
        if (SupplyOrderEnum.AFTER_SALE.getCode().equals(reqVO.getStatus())) {
            //出售
            if (SupplyInfoEnum.SELL.getCode().equals(reqVO.getType())) {
                //region 查询数据
                return supplyOrderMapper.selectJoinList(AppSupplyOrderRespVO.class, new MPJLambdaWrapperX<SupplyOrderDO>()
                        .eqIfExists(SupplyOrderDO::getCancelType, reqVO.getCancelType())
                        .eqIfExists(SupplyOrderDO::getTradeType, SupplyOrderEnum.GUARANTEE.getCode())
                        .eqIfExists(SupplyOrderDO::getPayStatus, reqVO.getPayStatus())
                        .eqIfExists(SupplyOrderDO::getDeliveryType, reqVO.getDeliveryType())
                        .eqIfExists(SupplyOrderDO::getOperateStatus, reqVO.getOperateStatus())
                        .eqIfExists(SupplyOrderDO::getSaleUserId, SecurityFrameworkUtils.getLoginUserId())
                        .in(SupplyOrderDO::getStatus,
                                SupplyOrderEnum.AFTER_SALE.getCode(),
                                //SupplyOrderEnum.COMPLETED.getCode(),
                                SupplyOrderEnum.REMITTED_MONEY_STATUS.getCode(),
                                SupplyOrderEnum.UNDER_RETURN_APPLICATION.getCode(),
                                SupplyOrderEnum.RETURNING_GOODS.getCode()//,
                                //SupplyOrderEnum.RETURNED.getCode()
                        )
                        .or(wrapper -> {
                            wrapper.eqIfExists(SupplyOrderDO::getCancelType, reqVO.getCancelType())
                                    .eqIfExists(SupplyOrderDO::getTradeType, SupplyOrderEnum.TAKE_RESPONSIBILITY_GUARANTEE.getCode())
                                    .eqIfExists(SupplyOrderDO::getAffirmDuty, SupplyOrderEnum.AFFIRM_DUTY.getCode())
                                    .eqIfExists(SupplyOrderDO::getPayStatus, reqVO.getPayStatus())
                                    .eqIfExists(SupplyOrderDO::getDeliveryType, reqVO.getDeliveryType())
                                    .eqIfExists(SupplyOrderDO::getOperateStatus, reqVO.getOperateStatus())
                                    .eqIfExists(SupplyOrderDO::getSaleUserId, SecurityFrameworkUtils.getLoginUserId())
                                    .in(SupplyOrderDO::getStatus,
                                            SupplyOrderEnum.AFTER_SALE.getCode(),
                                            //SupplyOrderEnum.COMPLETED.getCode(),
                                            SupplyOrderEnum.REMITTED_MONEY_STATUS.getCode(),
                                            SupplyOrderEnum.UNDER_RETURN_APPLICATION.getCode(),
                                            SupplyOrderEnum.RETURNING_GOODS.getCode()//,
                                            //SupplyOrderEnum.RETURNED.getCode()
                                    );
                        })
                        .leftJoin(MemberUserDO.class, "buy1", MemberUserDO::getId, SupplyOrderDO::getBuyUserId)
                        .leftJoin(BusinessUserDO.class, "buy2", BusinessUserDO::getUserId, SupplyOrderDO::getBuyUserId)
                        .leftJoin(BusinessCreditDO.class, "buy3", BusinessCreditDO::getId, "buy2", BusinessUserDO::getCreditId)
                        .leftJoin(MemberTypeDO.class, "buy4", MemberTypeDO::getId, "buy2", BusinessUserDO::getMemberTypeId)
                        .selectAs("buy3", BusinessCreditDO::getIcon, AppSupplyOrderRespVO::getBuyIcon)
                        .selectAs("buy1", MemberUserDO::getNickname, AppSupplyOrderRespVO::getBuyNickname)
                        .selectAs("buy1", MemberUserDO::getName, AppSupplyOrderRespVO::getBuyName)
                        .selectAs("buy1", MemberUserDO::getAvatar, AppSupplyOrderRespVO::getBuyAvatar)
                        .selectAs("buy4", MemberTypeDO::getName, AppSupplyOrderRespVO::getBuyMemberTypeName)
                        .selectAs("buy4", MemberTypeDO::getIcon, AppSupplyOrderRespVO::getBuyMemberTypeIcon)
                        .selectAs("buy2", BusinessUserDO::getAreaId, AppSupplyOrderRespVO::getBuyAreaId)
                        .selectAs("buy2", BusinessUserDO::getCompanyName, AppSupplyOrderRespVO::getBuyCompanyName)
                        .selectAs("buy2", BusinessUserDO::getReceiveAddress, AppSupplyOrderRespVO::getBuyReceiveAddress)

                        .leftJoin(MemberUserDO.class, "sale1", MemberUserDO::getId, SupplyOrderDO::getSaleUserId)
                        .leftJoin(BusinessUserDO.class, "sale2", BusinessUserDO::getUserId, SupplyOrderDO::getSaleUserId)
                        .leftJoin(BusinessCreditDO.class, "sale3", BusinessCreditDO::getId, "sale2", BusinessUserDO::getCreditId)
                        .leftJoin(MemberTypeDO.class, "sale4", MemberTypeDO::getId, "sale2", BusinessUserDO::getMemberTypeId)
                        .selectAll(SupplyOrderDO.class)
                        .selectAs("sale3", BusinessCreditDO::getIcon, AppSupplyOrderRespVO::getSaleIcon)
                        .selectAs("sale1", MemberUserDO::getName, AppSupplyOrderRespVO::getSaleName)
                        .selectAs("sale1", MemberUserDO::getNickname, AppSupplyOrderRespVO::getSaleNickname)
                        .selectAs("sale1", MemberUserDO::getAvatar, AppSupplyOrderRespVO::getSaleAvatar)
                        .selectAs("sale4", MemberTypeDO::getName, AppSupplyOrderRespVO::getSaleMemberTypeName)
                        .selectAs("sale4", MemberTypeDO::getIcon, AppSupplyOrderRespVO::getSaleMemberTypeIcon)
                        .selectAs("sale2", BusinessUserDO::getAreaId, AppSupplyOrderRespVO::getSaleAreaId)
                        .selectAs("sale2", BusinessUserDO::getCompanyName, AppSupplyOrderRespVO::getSaleCompanyName)
                        .selectAs("sale1", MemberUserDO::getMobile, AppSupplyOrderRespVO::getSaleMobile)
                        .selectAs("sale2", BusinessUserDO::getDeliveryAddress, AppSupplyOrderRespVO::getSaleDeliveryAddress)
                        .orderByDesc(SupplyOrderDO::getId));

                //endregion
            }
            //求购
            if (SupplyInfoEnum.ASK_TO_BUY.getCode().equals(reqVO.getType())) {
                //region 查询数据
                return supplyOrderMapper.selectJoinList(AppSupplyOrderRespVO.class, new MPJLambdaWrapperX<SupplyOrderDO>()
                        .eqIfExists(SupplyOrderDO::getTradeType, SupplyOrderEnum.GUARANTEE.getCode())
                        .eqIfExists(SupplyOrderDO::getCancelType, reqVO.getCancelType())
                        .eqIfExists(SupplyOrderDO::getPayStatus, reqVO.getPayStatus())
                        .eqIfExists(SupplyOrderDO::getDeliveryType, reqVO.getDeliveryType())
                        .eqIfExists(SupplyOrderDO::getOperateStatus, reqVO.getOperateStatus())
                        .eqIfExists(SupplyOrderDO::getBuyUserId, SecurityFrameworkUtils.getLoginUserId())
                        .in(SupplyOrderDO::getStatus,
                                SupplyOrderEnum.AFTER_SALE.getCode(),
                                //SupplyOrderEnum.COMPLETED.getCode(),
                                SupplyOrderEnum.REMITTED_MONEY_STATUS.getCode(),
                                SupplyOrderEnum.UNDER_RETURN_APPLICATION.getCode(),
                                SupplyOrderEnum.RETURNING_GOODS.getCode()//,
                                //SupplyOrderEnum.RETURNED.getCode()
                        )
                        .or(wrapper -> {
                            wrapper.eqIfExists(SupplyOrderDO::getTradeType, SupplyOrderEnum.TAKE_RESPONSIBILITY_GUARANTEE.getCode())
                                    .eqIfExists(SupplyOrderDO::getAffirmDuty, SupplyOrderEnum.AFFIRM_DUTY.getCode())
                                    .eqIfExists(SupplyOrderDO::getCancelType, reqVO.getCancelType())
                                    .eqIfExists(SupplyOrderDO::getPayStatus, reqVO.getPayStatus())
                                    .eqIfExists(SupplyOrderDO::getDeliveryType, reqVO.getDeliveryType())
                                    .eqIfExists(SupplyOrderDO::getOperateStatus, reqVO.getOperateStatus())
                                    .eqIfExists(SupplyOrderDO::getBuyUserId, SecurityFrameworkUtils.getLoginUserId())
                                    .in(SupplyOrderDO::getStatus,
                                            SupplyOrderEnum.AFTER_SALE.getCode(),
                                            //SupplyOrderEnum.COMPLETED.getCode(),
                                            SupplyOrderEnum.REMITTED_MONEY_STATUS.getCode(),
                                            SupplyOrderEnum.UNDER_RETURN_APPLICATION.getCode(),
                                            SupplyOrderEnum.RETURNING_GOODS.getCode()//,
                                            //SupplyOrderEnum.RETURNED.getCode()
                                    )
                            ;
                        })
                        .leftJoin(MemberUserDO.class, "buy1", MemberUserDO::getId, SupplyOrderDO::getBuyUserId)
                        .leftJoin(BusinessUserDO.class, "buy2", BusinessUserDO::getUserId, SupplyOrderDO::getBuyUserId)
                        .leftJoin(BusinessCreditDO.class, "buy3", BusinessCreditDO::getId, "buy2", BusinessUserDO::getCreditId)
                        .leftJoin(MemberTypeDO.class, "buy4", MemberTypeDO::getId, "buy2", BusinessUserDO::getMemberTypeId)
                        .selectAs("buy3", BusinessCreditDO::getIcon, AppSupplyOrderRespVO::getBuyIcon)
                        .selectAs("buy1", MemberUserDO::getNickname, AppSupplyOrderRespVO::getBuyNickname)
                        .selectAs("buy1", MemberUserDO::getName, AppSupplyOrderRespVO::getBuyName)
                        .selectAs("buy1", MemberUserDO::getAvatar, AppSupplyOrderRespVO::getBuyAvatar)
                        .selectAs("buy4", MemberTypeDO::getName, AppSupplyOrderRespVO::getBuyMemberTypeName)
                        .selectAs("buy4", MemberTypeDO::getIcon, AppSupplyOrderRespVO::getBuyMemberTypeIcon)
                        .selectAs("buy2", BusinessUserDO::getAreaId, AppSupplyOrderRespVO::getBuyAreaId)
                        .selectAs("buy2", BusinessUserDO::getCompanyName, AppSupplyOrderRespVO::getBuyCompanyName)
                        .selectAs("buy2", BusinessUserDO::getReceiveAddress, AppSupplyOrderRespVO::getBuyReceiveAddress)

                        .leftJoin(MemberUserDO.class, "sale1", MemberUserDO::getId, SupplyOrderDO::getSaleUserId)
                        .leftJoin(BusinessUserDO.class, "sale2", BusinessUserDO::getUserId, SupplyOrderDO::getSaleUserId)
                        .leftJoin(BusinessCreditDO.class, "sale3", BusinessCreditDO::getId, "sale2", BusinessUserDO::getCreditId)
                        .leftJoin(MemberTypeDO.class, "sale4", MemberTypeDO::getId, "sale2", BusinessUserDO::getMemberTypeId)
                        .selectAll(SupplyOrderDO.class)
                        .selectAs("sale3", BusinessCreditDO::getIcon, AppSupplyOrderRespVO::getSaleIcon)
                        .selectAs("sale1", MemberUserDO::getName, AppSupplyOrderRespVO::getSaleName)
                        .selectAs("sale1", MemberUserDO::getNickname, AppSupplyOrderRespVO::getSaleNickname)
                        .selectAs("sale1", MemberUserDO::getAvatar, AppSupplyOrderRespVO::getSaleAvatar)
                        .selectAs("sale4", MemberTypeDO::getName, AppSupplyOrderRespVO::getSaleMemberTypeName)
                        .selectAs("sale4", MemberTypeDO::getIcon, AppSupplyOrderRespVO::getSaleMemberTypeIcon)
                        .selectAs("sale2", BusinessUserDO::getAreaId, AppSupplyOrderRespVO::getSaleAreaId)
                        .selectAs("sale2", BusinessUserDO::getCompanyName, AppSupplyOrderRespVO::getSaleCompanyName)
                        .selectAs("sale1", MemberUserDO::getMobile, AppSupplyOrderRespVO::getSaleMobile)
                        .selectAs("sale2", BusinessUserDO::getDeliveryAddress, AppSupplyOrderRespVO::getSaleDeliveryAddress)
                        .orderByDesc(SupplyOrderDO::getId));
                //endregion
            }
        }
        //完成(买家收货完成+退货卖家收货完成)
        else if (SupplyOrderEnum.COMPLETED.getCode().equals(reqVO.getStatus())) {
            //出售
            if (SupplyInfoEnum.SELL.getCode().equals(reqVO.getType())) {
                //region 查询数据
                return supplyOrderMapper.selectJoinList(AppSupplyOrderRespVO.class, new MPJLambdaWrapperX<SupplyOrderDO>()
                        .eqIfExists(SupplyOrderDO::getCancelType, reqVO.getCancelType())
                        .eqIfExists(SupplyOrderDO::getTradeType, SupplyOrderEnum.GUARANTEE.getCode())
                        .eqIfExists(SupplyOrderDO::getPayStatus, reqVO.getPayStatus())
                        .eqIfExists(SupplyOrderDO::getDeliveryType, reqVO.getDeliveryType())
                        .eqIfExists(SupplyOrderDO::getOperateStatus, reqVO.getOperateStatus())
                        .eqIfExists(SupplyOrderDO::getSaleUserId, SecurityFrameworkUtils.getLoginUserId())
                        .in(SupplyOrderDO::getStatus,
                                SupplyOrderEnum.COMPLETED.getCode(),
                                SupplyOrderEnum.RETURNED.getCode()
                        )
                        .or(wrapper -> {
                            wrapper.eqIfExists(SupplyOrderDO::getCancelType, reqVO.getCancelType())
                                    .eqIfExists(SupplyOrderDO::getTradeType, SupplyOrderEnum.TAKE_RESPONSIBILITY_GUARANTEE.getCode())
                                    .eqIfExists(SupplyOrderDO::getAffirmDuty, SupplyOrderEnum.AFFIRM_DUTY.getCode())
                                    .eqIfExists(SupplyOrderDO::getPayStatus, reqVO.getPayStatus())
                                    .eqIfExists(SupplyOrderDO::getDeliveryType, reqVO.getDeliveryType())
                                    .eqIfExists(SupplyOrderDO::getOperateStatus, reqVO.getOperateStatus())
                                    .eqIfExists(SupplyOrderDO::getSaleUserId, SecurityFrameworkUtils.getLoginUserId())
                                    .in(SupplyOrderDO::getStatus,
                                            SupplyOrderEnum.COMPLETED.getCode(),
                                            SupplyOrderEnum.RETURNED.getCode()
                                    );
                        })
                        .leftJoin(MemberUserDO.class, "buy1", MemberUserDO::getId, SupplyOrderDO::getBuyUserId)
                        .leftJoin(BusinessUserDO.class, "buy2", BusinessUserDO::getUserId, SupplyOrderDO::getBuyUserId)
                        .leftJoin(BusinessCreditDO.class, "buy3", BusinessCreditDO::getId, "buy2", BusinessUserDO::getCreditId)
                        .leftJoin(MemberTypeDO.class, "buy4", MemberTypeDO::getId, "buy2", BusinessUserDO::getMemberTypeId)
                        .selectAs("buy3", BusinessCreditDO::getIcon, AppSupplyOrderRespVO::getBuyIcon)
                        .selectAs("buy1", MemberUserDO::getNickname, AppSupplyOrderRespVO::getBuyNickname)
                        .selectAs("buy1", MemberUserDO::getName, AppSupplyOrderRespVO::getBuyName)
                        .selectAs("buy1", MemberUserDO::getAvatar, AppSupplyOrderRespVO::getBuyAvatar)
                        .selectAs("buy4", MemberTypeDO::getName, AppSupplyOrderRespVO::getBuyMemberTypeName)
                        .selectAs("buy4", MemberTypeDO::getIcon, AppSupplyOrderRespVO::getBuyMemberTypeIcon)
                        .selectAs("buy2", BusinessUserDO::getAreaId, AppSupplyOrderRespVO::getBuyAreaId)
                        .selectAs("buy2", BusinessUserDO::getCompanyName, AppSupplyOrderRespVO::getBuyCompanyName)
                        .selectAs("buy2", BusinessUserDO::getReceiveAddress, AppSupplyOrderRespVO::getBuyReceiveAddress)

                        .leftJoin(MemberUserDO.class, "sale1", MemberUserDO::getId, SupplyOrderDO::getSaleUserId)
                        .leftJoin(BusinessUserDO.class, "sale2", BusinessUserDO::getUserId, SupplyOrderDO::getSaleUserId)
                        .leftJoin(BusinessCreditDO.class, "sale3", BusinessCreditDO::getId, "sale2", BusinessUserDO::getCreditId)
                        .leftJoin(MemberTypeDO.class, "sale4", MemberTypeDO::getId, "sale2", BusinessUserDO::getMemberTypeId)
                        .selectAll(SupplyOrderDO.class)
                        .selectAs("sale3", BusinessCreditDO::getIcon, AppSupplyOrderRespVO::getSaleIcon)
                        .selectAs("sale1", MemberUserDO::getName, AppSupplyOrderRespVO::getSaleName)
                        .selectAs("sale1", MemberUserDO::getNickname, AppSupplyOrderRespVO::getSaleNickname)
                        .selectAs("sale1", MemberUserDO::getAvatar, AppSupplyOrderRespVO::getSaleAvatar)
                        .selectAs("sale4", MemberTypeDO::getName, AppSupplyOrderRespVO::getSaleMemberTypeName)
                        .selectAs("sale4", MemberTypeDO::getIcon, AppSupplyOrderRespVO::getSaleMemberTypeIcon)
                        .selectAs("sale2", BusinessUserDO::getAreaId, AppSupplyOrderRespVO::getSaleAreaId)
                        .selectAs("sale2", BusinessUserDO::getCompanyName, AppSupplyOrderRespVO::getSaleCompanyName)
                        .selectAs("sale1", MemberUserDO::getMobile, AppSupplyOrderRespVO::getSaleMobile)
                        .selectAs("sale2", BusinessUserDO::getDeliveryAddress, AppSupplyOrderRespVO::getSaleDeliveryAddress)
                        .orderByDesc(SupplyOrderDO::getId));

                //endregion
            }
            //求购
            if (SupplyInfoEnum.ASK_TO_BUY.getCode().equals(reqVO.getType())) {
                //region 查询数据
                return supplyOrderMapper.selectJoinList(AppSupplyOrderRespVO.class, new MPJLambdaWrapperX<SupplyOrderDO>()
                        .eqIfExists(SupplyOrderDO::getTradeType, SupplyOrderEnum.GUARANTEE.getCode())
                        .eqIfExists(SupplyOrderDO::getCancelType, reqVO.getCancelType())
                        .eqIfExists(SupplyOrderDO::getPayStatus, reqVO.getPayStatus())
                        .eqIfExists(SupplyOrderDO::getDeliveryType, reqVO.getDeliveryType())
                        .eqIfExists(SupplyOrderDO::getOperateStatus, reqVO.getOperateStatus())
                        .eqIfExists(SupplyOrderDO::getBuyUserId, SecurityFrameworkUtils.getLoginUserId())
                        .in(SupplyOrderDO::getStatus,
                                SupplyOrderEnum.COMPLETED.getCode(),
                                SupplyOrderEnum.RETURNED.getCode()
                        )
                        .or(wrapper -> {
                            wrapper.eqIfExists(SupplyOrderDO::getTradeType, SupplyOrderEnum.TAKE_RESPONSIBILITY_GUARANTEE.getCode())
                                    .eqIfExists(SupplyOrderDO::getAffirmDuty, SupplyOrderEnum.AFFIRM_DUTY.getCode())
                                    .eqIfExists(SupplyOrderDO::getCancelType, reqVO.getCancelType())
                                    .eqIfExists(SupplyOrderDO::getPayStatus, reqVO.getPayStatus())
                                    .eqIfExists(SupplyOrderDO::getDeliveryType, reqVO.getDeliveryType())
                                    .eqIfExists(SupplyOrderDO::getOperateStatus, reqVO.getOperateStatus())
                                    .eqIfExists(SupplyOrderDO::getBuyUserId, SecurityFrameworkUtils.getLoginUserId())
                                    .in(SupplyOrderDO::getStatus,
                                            SupplyOrderEnum.COMPLETED.getCode(),
                                            SupplyOrderEnum.RETURNED.getCode()
                                    )
                            ;
                        })
                        .leftJoin(MemberUserDO.class, "buy1", MemberUserDO::getId, SupplyOrderDO::getBuyUserId)
                        .leftJoin(BusinessUserDO.class, "buy2", BusinessUserDO::getUserId, SupplyOrderDO::getBuyUserId)
                        .leftJoin(BusinessCreditDO.class, "buy3", BusinessCreditDO::getId, "buy2", BusinessUserDO::getCreditId)
                        .leftJoin(MemberTypeDO.class, "buy4", MemberTypeDO::getId, "buy2", BusinessUserDO::getMemberTypeId)
                        .selectAs("buy3", BusinessCreditDO::getIcon, AppSupplyOrderRespVO::getBuyIcon)
                        .selectAs("buy1", MemberUserDO::getNickname, AppSupplyOrderRespVO::getBuyNickname)
                        .selectAs("buy1", MemberUserDO::getName, AppSupplyOrderRespVO::getBuyName)
                        .selectAs("buy1", MemberUserDO::getAvatar, AppSupplyOrderRespVO::getBuyAvatar)
                        .selectAs("buy4", MemberTypeDO::getName, AppSupplyOrderRespVO::getBuyMemberTypeName)
                        .selectAs("buy4", MemberTypeDO::getIcon, AppSupplyOrderRespVO::getBuyMemberTypeIcon)
                        .selectAs("buy2", BusinessUserDO::getAreaId, AppSupplyOrderRespVO::getBuyAreaId)
                        .selectAs("buy2", BusinessUserDO::getCompanyName, AppSupplyOrderRespVO::getBuyCompanyName)
                        .selectAs("buy2", BusinessUserDO::getReceiveAddress, AppSupplyOrderRespVO::getBuyReceiveAddress)

                        .leftJoin(MemberUserDO.class, "sale1", MemberUserDO::getId, SupplyOrderDO::getSaleUserId)
                        .leftJoin(BusinessUserDO.class, "sale2", BusinessUserDO::getUserId, SupplyOrderDO::getSaleUserId)
                        .leftJoin(BusinessCreditDO.class, "sale3", BusinessCreditDO::getId, "sale2", BusinessUserDO::getCreditId)
                        .leftJoin(MemberTypeDO.class, "sale4", MemberTypeDO::getId, "sale2", BusinessUserDO::getMemberTypeId)
                        .selectAll(SupplyOrderDO.class)
                        .selectAs("sale3", BusinessCreditDO::getIcon, AppSupplyOrderRespVO::getSaleIcon)
                        .selectAs("sale1", MemberUserDO::getName, AppSupplyOrderRespVO::getSaleName)
                        .selectAs("sale1", MemberUserDO::getNickname, AppSupplyOrderRespVO::getSaleNickname)
                        .selectAs("sale1", MemberUserDO::getAvatar, AppSupplyOrderRespVO::getSaleAvatar)
                        .selectAs("sale4", MemberTypeDO::getName, AppSupplyOrderRespVO::getSaleMemberTypeName)
                        .selectAs("sale4", MemberTypeDO::getIcon, AppSupplyOrderRespVO::getSaleMemberTypeIcon)
                        .selectAs("sale2", BusinessUserDO::getAreaId, AppSupplyOrderRespVO::getSaleAreaId)
                        .selectAs("sale2", BusinessUserDO::getCompanyName, AppSupplyOrderRespVO::getSaleCompanyName)
                        .selectAs("sale1", MemberUserDO::getMobile, AppSupplyOrderRespVO::getSaleMobile)
                        .selectAs("sale2", BusinessUserDO::getDeliveryAddress, AppSupplyOrderRespVO::getSaleDeliveryAddress)
                        .orderByDesc(SupplyOrderDO::getId));
                //endregion
            }
        }
        else {
            //出售
            if (SupplyInfoEnum.SELL.getCode().equals(reqVO.getType())) {
                //region 查询数据
                return supplyOrderMapper.selectJoinList(AppSupplyOrderRespVO.class, new MPJLambdaWrapperX<SupplyOrderDO>()
                        .eqIfExists(SupplyOrderDO::getTradeType, SupplyOrderEnum.GUARANTEE.getCode())
                        .eqIfExists(SupplyOrderDO::getCancelType, reqVO.getCancelType())
                        .eqIfExists(SupplyOrderDO::getPayStatus, reqVO.getPayStatus())
                        .eqIfExists(SupplyOrderDO::getDeliveryType, reqVO.getDeliveryType())
                        .eqIfExists(SupplyOrderDO::getOperateStatus, reqVO.getOperateStatus())
                        .eqIfExists(SupplyOrderDO::getSaleUserId, SecurityFrameworkUtils.getLoginUserId())
                        .eqIfExists(SupplyOrderDO::getStatus, reqVO.getStatus())

                        .or(wrapper -> {
                            wrapper.eqIfExists(SupplyOrderDO::getTradeType, SupplyOrderEnum.TAKE_RESPONSIBILITY_GUARANTEE.getCode())
                                    .eqIfExists(SupplyOrderDO::getAffirmDuty, SupplyOrderEnum.AFFIRM_DUTY.getCode())
                                    .eqIfExists(SupplyOrderDO::getCancelType, reqVO.getCancelType())
                                    .eqIfExists(SupplyOrderDO::getPayStatus, reqVO.getPayStatus())
                                    .eqIfExists(SupplyOrderDO::getDeliveryType, reqVO.getDeliveryType())
                                    .eqIfExists(SupplyOrderDO::getOperateStatus, reqVO.getOperateStatus())
                                    .eqIfExists(SupplyOrderDO::getSaleUserId, SecurityFrameworkUtils.getLoginUserId())
                                    .eqIfExists(SupplyOrderDO::getStatus, reqVO.getStatus());
                        })
                        .leftJoin(MemberUserDO.class, "buy1", MemberUserDO::getId, SupplyOrderDO::getBuyUserId)
                        .leftJoin(BusinessUserDO.class, "buy2", BusinessUserDO::getUserId, SupplyOrderDO::getBuyUserId)
                        .leftJoin(BusinessCreditDO.class, "buy3", BusinessCreditDO::getId, "buy2", BusinessUserDO::getCreditId)
                        .leftJoin(MemberTypeDO.class, "buy4", MemberTypeDO::getId, "buy2", BusinessUserDO::getMemberTypeId)
                        .selectAs("buy3", BusinessCreditDO::getIcon, AppSupplyOrderRespVO::getBuyIcon)
                        .selectAs("buy1", MemberUserDO::getNickname, AppSupplyOrderRespVO::getBuyNickname)
                        .selectAs("buy1", MemberUserDO::getName, AppSupplyOrderRespVO::getBuyName)
                        .selectAs("buy1", MemberUserDO::getAvatar, AppSupplyOrderRespVO::getBuyAvatar)
                        .selectAs("buy4", MemberTypeDO::getName, AppSupplyOrderRespVO::getBuyMemberTypeName)
                        .selectAs("buy4", MemberTypeDO::getIcon, AppSupplyOrderRespVO::getBuyMemberTypeIcon)
                        .selectAs("buy2", BusinessUserDO::getAreaId, AppSupplyOrderRespVO::getBuyAreaId)
                        .selectAs("buy2", BusinessUserDO::getCompanyName, AppSupplyOrderRespVO::getBuyCompanyName)
                        .selectAs("buy2", BusinessUserDO::getReceiveAddress, AppSupplyOrderRespVO::getBuyReceiveAddress)

                        .leftJoin(MemberUserDO.class, "sale1", MemberUserDO::getId, SupplyOrderDO::getSaleUserId)
                        .leftJoin(BusinessUserDO.class, "sale2", BusinessUserDO::getUserId, SupplyOrderDO::getSaleUserId)
                        .leftJoin(BusinessCreditDO.class, "sale3", BusinessCreditDO::getId, "sale2", BusinessUserDO::getCreditId)
                        .leftJoin(MemberTypeDO.class, "sale4", MemberTypeDO::getId, "sale2", BusinessUserDO::getMemberTypeId)
                        .selectAll(SupplyOrderDO.class)
                        .selectAs("sale3", BusinessCreditDO::getIcon, AppSupplyOrderRespVO::getSaleIcon)
                        .selectAs("sale1", MemberUserDO::getName, AppSupplyOrderRespVO::getSaleName)
                        .selectAs("sale1", MemberUserDO::getNickname, AppSupplyOrderRespVO::getSaleNickname)
                        .selectAs("sale1", MemberUserDO::getAvatar, AppSupplyOrderRespVO::getSaleAvatar)
                        .selectAs("sale4", MemberTypeDO::getName, AppSupplyOrderRespVO::getSaleMemberTypeName)
                        .selectAs("sale4", MemberTypeDO::getIcon, AppSupplyOrderRespVO::getSaleMemberTypeIcon)
                        .selectAs("sale2", BusinessUserDO::getAreaId, AppSupplyOrderRespVO::getSaleAreaId)
                        .selectAs("sale2", BusinessUserDO::getCompanyName, AppSupplyOrderRespVO::getSaleCompanyName)
                        .selectAs("sale1", MemberUserDO::getMobile, AppSupplyOrderRespVO::getSaleMobile)
                        .selectAs("sale2", BusinessUserDO::getDeliveryAddress, AppSupplyOrderRespVO::getSaleDeliveryAddress)

                        .orderByDesc(SupplyOrderDO::getId));
                //endregion
            }
            //求购
            if (SupplyInfoEnum.ASK_TO_BUY.getCode().equals(reqVO.getType())) {
                //region 查询数据
                return supplyOrderMapper.selectJoinList(AppSupplyOrderRespVO.class, new MPJLambdaWrapperX<SupplyOrderDO>()
                        .eqIfExists(SupplyOrderDO::getTradeType, SupplyOrderEnum.GUARANTEE.getCode())
                        .eqIfExists(SupplyOrderDO::getCancelType, reqVO.getCancelType())
                        .eqIfExists(SupplyOrderDO::getPayStatus, reqVO.getPayStatus())
                        .eqIfExists(SupplyOrderDO::getDeliveryType, reqVO.getDeliveryType())
                        .eqIfExists(SupplyOrderDO::getOperateStatus, reqVO.getOperateStatus())
                        .eqIfExists(SupplyOrderDO::getBuyUserId, SecurityFrameworkUtils.getLoginUserId())
                        .eqIfExists(SupplyOrderDO::getStatus, reqVO.getStatus())
                        .or(wrapper -> {
                            wrapper.eqIfExists(SupplyOrderDO::getTradeType, SupplyOrderEnum.TAKE_RESPONSIBILITY_GUARANTEE.getCode())
                                    .eqIfExists(SupplyOrderDO::getAffirmDuty, SupplyOrderEnum.AFFIRM_DUTY.getCode())
                                    .eqIfExists(SupplyOrderDO::getCancelType, reqVO.getCancelType())
                                    .eqIfExists(SupplyOrderDO::getPayStatus, reqVO.getPayStatus())
                                    .eqIfExists(SupplyOrderDO::getDeliveryType, reqVO.getDeliveryType())
                                    .eqIfExists(SupplyOrderDO::getOperateStatus, reqVO.getOperateStatus())
                                    .eqIfExists(SupplyOrderDO::getBuyUserId, SecurityFrameworkUtils.getLoginUserId())
                                    .eqIfExists(SupplyOrderDO::getStatus, reqVO.getStatus());
                        })
                        .leftJoin(MemberUserDO.class, "buy1", MemberUserDO::getId, SupplyOrderDO::getBuyUserId)
                        .leftJoin(BusinessUserDO.class, "buy2", BusinessUserDO::getUserId, SupplyOrderDO::getBuyUserId)
                        .leftJoin(BusinessCreditDO.class, "buy3", BusinessCreditDO::getId, "buy2", BusinessUserDO::getCreditId)
                        .leftJoin(MemberTypeDO.class, "buy4", MemberTypeDO::getId, "buy2", BusinessUserDO::getMemberTypeId)
                        .selectAs("buy3", BusinessCreditDO::getIcon, AppSupplyOrderRespVO::getBuyIcon)
                        .selectAs("buy1", MemberUserDO::getNickname, AppSupplyOrderRespVO::getBuyNickname)
                        .selectAs("buy1", MemberUserDO::getName, AppSupplyOrderRespVO::getBuyName)
                        .selectAs("buy1", MemberUserDO::getAvatar, AppSupplyOrderRespVO::getBuyAvatar)
                        .selectAs("buy4", MemberTypeDO::getName, AppSupplyOrderRespVO::getBuyMemberTypeName)
                        .selectAs("buy4", MemberTypeDO::getIcon, AppSupplyOrderRespVO::getBuyMemberTypeIcon)
                        .selectAs("buy2", BusinessUserDO::getAreaId, AppSupplyOrderRespVO::getBuyAreaId)
                        .selectAs("buy2", BusinessUserDO::getCompanyName, AppSupplyOrderRespVO::getBuyCompanyName)
                        .selectAs("buy2", BusinessUserDO::getReceiveAddress, AppSupplyOrderRespVO::getBuyReceiveAddress)

                        .leftJoin(MemberUserDO.class, "sale1", MemberUserDO::getId, SupplyOrderDO::getSaleUserId)
                        .leftJoin(BusinessUserDO.class, "sale2", BusinessUserDO::getUserId, SupplyOrderDO::getSaleUserId)
                        .leftJoin(BusinessCreditDO.class, "sale3", BusinessCreditDO::getId, "sale2", BusinessUserDO::getCreditId)
                        .leftJoin(MemberTypeDO.class, "sale4", MemberTypeDO::getId, "sale2", BusinessUserDO::getMemberTypeId)
                        .selectAll(SupplyOrderDO.class)
                        .selectAs("sale3", BusinessCreditDO::getIcon, AppSupplyOrderRespVO::getSaleIcon)
                        .selectAs("sale1", MemberUserDO::getName, AppSupplyOrderRespVO::getSaleName)
                        .selectAs("sale1", MemberUserDO::getNickname, AppSupplyOrderRespVO::getSaleNickname)
                        .selectAs("sale1", MemberUserDO::getAvatar, AppSupplyOrderRespVO::getSaleAvatar)
                        .selectAs("sale4", MemberTypeDO::getName, AppSupplyOrderRespVO::getSaleMemberTypeName)
                        .selectAs("sale4", MemberTypeDO::getIcon, AppSupplyOrderRespVO::getSaleMemberTypeIcon)
                        .selectAs("sale2", BusinessUserDO::getAreaId, AppSupplyOrderRespVO::getSaleAreaId)
                        .selectAs("sale2", BusinessUserDO::getCompanyName, AppSupplyOrderRespVO::getSaleCompanyName)
                        .selectAs("sale1", MemberUserDO::getMobile, AppSupplyOrderRespVO::getSaleMobile)
                        .selectAs("sale2", BusinessUserDO::getDeliveryAddress, AppSupplyOrderRespVO::getSaleDeliveryAddress)

                        .orderByDesc(SupplyOrderDO::getId));
                //endregion
            }
        }

        //region 查询数据
        return supplyOrderMapper.selectJoinList(AppSupplyOrderRespVO.class, new MPJLambdaWrapperX<SupplyOrderDO>()
                .eqIfExists(SupplyOrderDO::getTradeType, SupplyOrderEnum.GUARANTEE.getCode())
                .eqIfExists(SupplyOrderDO::getType, reqVO.getType())
                .eqIfExists(SupplyOrderDO::getStatus, reqVO.getStatus())
                .eqIfExists(SupplyOrderDO::getCancelType, reqVO.getCancelType())
                .eqIfExists(SupplyOrderDO::getPayStatus, reqVO.getPayStatus())
                .eqIfExists(SupplyOrderDO::getDeliveryType, reqVO.getDeliveryType())
                .eqIfExists(SupplyOrderDO::getOperateStatus, reqVO.getOperateStatus())
                .eqIfExists(SupplyOrderDO::getBuyUserId, SecurityFrameworkUtils.getLoginUserId())
                .or(wrapper -> {
                    wrapper.eqIfExists(SupplyOrderDO::getTradeType, SupplyOrderEnum.TAKE_RESPONSIBILITY_GUARANTEE.getCode())
                            .eqIfExists(SupplyOrderDO::getAffirmDuty, SupplyOrderEnum.AFFIRM_DUTY.getCode())
                            .eqIfExists(SupplyOrderDO::getType, reqVO.getType())
                            .eqIfExists(SupplyOrderDO::getStatus, reqVO.getStatus())
                            .eqIfExists(SupplyOrderDO::getCancelType, reqVO.getCancelType())
                            .eqIfExists(SupplyOrderDO::getPayStatus, reqVO.getPayStatus())
                            .eqIfExists(SupplyOrderDO::getDeliveryType, reqVO.getDeliveryType())
                            .eqIfExists(SupplyOrderDO::getOperateStatus, reqVO.getOperateStatus())
                            .eqIfExists(SupplyOrderDO::getBuyUserId, SecurityFrameworkUtils.getLoginUserId());
                })
                .or(wrapper -> {
                    wrapper.eqIfExists(SupplyOrderDO::getTradeType, SupplyOrderEnum.GUARANTEE.getCode())
                            .eqIfExists(SupplyOrderDO::getType, reqVO.getType())
                            .eqIfExists(SupplyOrderDO::getStatus, reqVO.getStatus())
                            .eqIfExists(SupplyOrderDO::getCancelType, reqVO.getCancelType())
                            .eqIfExists(SupplyOrderDO::getPayStatus, reqVO.getPayStatus())
                            .eqIfExists(SupplyOrderDO::getDeliveryType, reqVO.getDeliveryType())
                            .eqIfExists(SupplyOrderDO::getOperateStatus, reqVO.getOperateStatus())
                            .eqIfExists(SupplyOrderDO::getSaleUserId, SecurityFrameworkUtils.getLoginUserId());
                })
                .or(wrapper -> {
                    wrapper.eqIfExists(SupplyOrderDO::getTradeType, SupplyOrderEnum.TAKE_RESPONSIBILITY_GUARANTEE.getCode())
                            .eqIfExists(SupplyOrderDO::getAffirmDuty, SupplyOrderEnum.AFFIRM_DUTY.getCode())
                            .eqIfExists(SupplyOrderDO::getType, reqVO.getType())
                            .eqIfExists(SupplyOrderDO::getStatus, reqVO.getStatus())
                            .eqIfExists(SupplyOrderDO::getCancelType, reqVO.getCancelType())
                            .eqIfExists(SupplyOrderDO::getPayStatus, reqVO.getPayStatus())
                            .eqIfExists(SupplyOrderDO::getDeliveryType, reqVO.getDeliveryType())
                            .eqIfExists(SupplyOrderDO::getOperateStatus, reqVO.getOperateStatus())
                            .eqIfExists(SupplyOrderDO::getSaleUserId, SecurityFrameworkUtils.getLoginUserId());
                })
                .leftJoin(MemberUserDO.class, "buy1", MemberUserDO::getId, SupplyOrderDO::getBuyUserId)
                .leftJoin(BusinessUserDO.class, "buy2", BusinessUserDO::getUserId, SupplyOrderDO::getBuyUserId)
                .leftJoin(BusinessCreditDO.class, "buy3", BusinessCreditDO::getId, "buy2", BusinessUserDO::getCreditId)
                .leftJoin(MemberTypeDO.class, "buy4", MemberTypeDO::getId, "buy2", BusinessUserDO::getMemberTypeId)

                .leftJoin(MemberUserDO.class, "sale1", MemberUserDO::getId, SupplyOrderDO::getSaleUserId)
                .leftJoin(BusinessUserDO.class, "sale2", BusinessUserDO::getUserId, SupplyOrderDO::getSaleUserId)
                .leftJoin(BusinessCreditDO.class, "sale3", BusinessCreditDO::getId, "sale2", BusinessUserDO::getCreditId)
                .leftJoin(MemberTypeDO.class, "sale4", MemberTypeDO::getId, "sale2", BusinessUserDO::getMemberTypeId)
                .selectAll(SupplyOrderDO.class)
                .selectAs("buy3", BusinessCreditDO::getIcon, AppSupplyOrderRespVO::getBuyIcon)
                .selectAs("buy1", MemberUserDO::getNickname, AppSupplyOrderRespVO::getBuyNickname)
                .selectAs("buy1", MemberUserDO::getName, AppSupplyOrderRespVO::getBuyName)
                .selectAs("buy1", MemberUserDO::getAvatar, AppSupplyOrderRespVO::getBuyAvatar)
                .selectAs("buy4", MemberTypeDO::getName, AppSupplyOrderRespVO::getBuyMemberTypeName)
                .selectAs("buy4", MemberTypeDO::getIcon, AppSupplyOrderRespVO::getBuyMemberTypeIcon)
                .selectAs("buy2", BusinessUserDO::getAreaId, AppSupplyOrderRespVO::getBuyAreaId)
                .selectAs("buy2", BusinessUserDO::getCompanyName, AppSupplyOrderRespVO::getBuyCompanyName)
                .selectAs("buy2", BusinessUserDO::getDeliveryAddress, AppSupplyOrderRespVO::getBuyReceiveAddress)

                .selectAs("sale3", BusinessCreditDO::getIcon, AppSupplyOrderRespVO::getSaleIcon)
                .selectAs("sale1", MemberUserDO::getName, AppSupplyOrderRespVO::getSaleName)
                .selectAs("sale1", MemberUserDO::getNickname, AppSupplyOrderRespVO::getSaleNickname)
                .selectAs("sale1", MemberUserDO::getAvatar, AppSupplyOrderRespVO::getSaleAvatar)
                .selectAs("sale4", MemberTypeDO::getName, AppSupplyOrderRespVO::getSaleMemberTypeName)
                .selectAs("sale4", MemberTypeDO::getIcon, AppSupplyOrderRespVO::getSaleMemberTypeIcon)
                .selectAs("sale2", BusinessUserDO::getAreaId, AppSupplyOrderRespVO::getSaleAreaId)
                .selectAs("sale2", BusinessUserDO::getCompanyName, AppSupplyOrderRespVO::getSaleCompanyName)
                .selectAs("sale1", MemberUserDO::getMobile, AppSupplyOrderRespVO::getSaleMobile)
                .selectAs("sale2", BusinessUserDO::getDeliveryAddress, AppSupplyOrderRespVO::getSaleDeliveryAddress)

                .orderByDesc(SupplyOrderDO::getId));
        //endregion
    }
}